package model.forview;

import java.util.ArrayList;

import controllers.routes;

import model.wrap.node.Node;
import model.wrap.structure.Structure;
import models.ConstantName;
import models.ConstantValue;

/**
 * 
 * @author pocteg
 * 
 */
public class NodeRelationAddModelForView extends ContentForMainTemplate
{

    /**
     * ----PRIVATE-----------ATTRIBUTS-----------INSTANCE----------------------
     */

    private Node node;
    private Node relative;
    private Structure headStructure;

    private ArrayList<Node> parentsOfRelative;
    private ArrayList<Node> childrenOfRelative;
    private ArrayList<Structure> allStructures;

    private boolean isRoot;

    /**
     * ----PRIVATE-----------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----PROTECTED---------ATTRIBUTS-----------INSTANCE----------------------
     */

    /**
     * ----PROTECTED---------ATTRIBUTS-----------STATIC------------------------
     */

    /**
     * ----PUBLIC------------ATTRIBUTS-----------INSTANCE----------------------
     */

    public Long idNode;
    public Long idRelative;

    /**
     * If type = "c" add relative like child else add parent
     */
    public String type;

    /**
     * ----PUBLIC------------ATTRIBUTS-----------STATIC------------------------
     */

    public final String ID_NODE = "idNode";
    public final String ID_RELATIVE = "idRelative";
    public final String TYPE = "type";
    public final String TYPE_C = "c";
    public final String TYPE_P = "p";

    /**
     * ----CONSTRUCTORS--------------------------------------------------------
     */

    public NodeRelationAddModelForView()
    {

    }

    /**
     * 
     * @param node
     */
    public NodeRelationAddModelForView(Node node, Node relative, Structure structure)
    {
	this.headStructure = structure;
	setRelative(node, relative);
	setDefaultValues();
    }

    /**
     * ----PRIVATE-----------METHOD-----------INSTANCE-------------------------
     */

    /**
     * @author pocteg
     */
    private void setDefaultValues()
    {
	String title = ConstantValue.getConstantValue(ConstantName.NODE_RELATION_ADD_PAGE_TITLE);
	String header = ConstantValue.getConstantValue(ConstantName.NODE_RELATION_ADD_PAGE_HEADER);
	String footer = ConstantValue.getConstantValue(ConstantName.NODE_RELATION_ADD_PAGE_FOOTER);
	String userName = "";

	title = title != null ? title : "Редактирование связей узла";
	header = header != null ? header : "Редактирование связей узла";
	footer = footer != null ? footer : "Редактирование связей узла";

	mainModelForView = new MainModelForView(title, userName, header, footer);
    }

    /**
     * Set other values
     * 
     * @author pocteg
     * @param node
     */
    private void setRelative(Node node, Node relative)
    {
	this.node = node;
	this.relative = relative;
	this.allStructures = Structure.getAllStructures();
	if (relative != null)
	{
	    this.parentsOfRelative = relative.getParents();
	    this.childrenOfRelative = relative.getChildren();
	    this.isRoot = false;
	}
	else
	{
	    if (null != headStructure)
		this.parentsOfRelative = Node.getRootNodes(headStructure);
	    else
		this.parentsOfRelative = Node.getRootNodes(node.getStructure());
	    this.childrenOfRelative = new ArrayList<Node>();
	    this.isRoot = true;
	}
    }

    /**
     * ----PRIVATE-----------METHOD-----------STATIC---------------------------
     */

    /**
     * ----PROTECTED---------METHOD-----------INSTANCE-------------------------
     */

    /**
     * ----PROTECTED---------METHOD-----------STATIC---------------------------
     */

    /**
     * ----PUBLIC------------METHOD-----------INSTANCE-------------------------
     */

    /**
     * Get size of parent
     * 
     * @author pocteg
     * @return
     */
    public int getParentsOfRelativeSize()
    {
	return this.parentsOfRelative.size();
    }

    /**
     * Get size of children
     * 
     * @author pocteg
     * @return
     */
    public int getChildrenOfRelativeSize()
    {
	return this.childrenOfRelative.size();
    }

    /**
     * Get title of child.
     * 
     * @author pocteg
     * @param index
     * @return
     */
    public String getChildOfRelativeTitle(int index)
    {
	return this.childrenOfRelative.get(index).getTitle();
    }

    /**
     * @author pocteg
     * @param index
     * @return
     */
    public String getChildOfRelativeUrl(int index)
    {
	return routes.Hierarchy.getPageNodeRelationAdd(this.node.getId(), this.childrenOfRelative.get(index).getId(), new Long(0)).url();
    }

    /**
     * Get title of parent.
     * 
     * @author pocteg
     * @param index
     * @return
     */
    public String getParentOfRelativeTitle(int index)
    {
	return this.parentsOfRelative.get(index).getTitle();
    }

    public String getParentOfRelativeUrl(int index)
    {
	return routes.Hierarchy.getPageNodeRelationAdd(this.node.getId(), this.parentsOfRelative.get(index).getId(), new Long(0)).url();
    }

    /**
     * Get title of node.
     * 
     * @author pocteg
     * @return
     */
    public String getNodeTitle()
    {
	return this.node.getTitle();
    }
    
    public String getStructureTitle()
    {
	return this.node.getStructure().getTitle();
    }
    
    /**
     * Get title of relative.
     * 
     * @author pocteg
     * @return
     */
    public String getRelativeTitle()
    {
	String title = "";
	if (this.relative != null) title = this.relative.getTitle();
	return title;
    }

    /**
     * Get id of node.
     * 
     * @author pocteg
     * @return
     */
    public Long getNodeId()
    {
	return this.node.getId();
    }

    public Long getStructureId()
    {
	return this.node.getStructure().getId();
    }
    
    /**
     * Get id of relative.
     * 
     * @author pocteg
     * @return
     */
    public Long getRelativeId()
    {
	if (isRoot == true) return new Long(0);
	return this.relative.getId();
    }
    
    public String getRelativeStructureTitle()
    {
	if(true == isRoot) return "";
	return this.relative.getStructure().getTitle();
    }
    
    public Long getRelativeStructureId()
    {
	if(true == isRoot) return new Long(0);
	return this.relative.getStructure().getId();
    }
    
    public String getRootUrl()
    {
	return routes.Hierarchy.getPageNodeRelationAdd(this.node.getId(), 0, new Long(0)).url();
    }

    public String getRelationPage()
    {
	return routes.Hierarchy.getPageNodeRelation(this.node.getId()).url();
    }
    
    public int getSizeOfAllStructures()
    {
	return this.allStructures.size();
    }
    
    public String getTitleOfStructure(int index)
    {
	return this.allStructures.get(index).getTitle();
    }
    
    public Long getIdOfStructure(int index)
    {
	return this.allStructures.get(index).getId();
    }

    
    public ArrayList<Node> getParentsOfRelative()
    {
	return this.parentsOfRelative;
    }
    
    public ArrayList<Node> getChildrenOfRelative()
    {
	return this.childrenOfRelative;
    }
    /**
     * ----PUBLIC------------METHOD-----------STATIC---------------------------
     */

    /**
     * ----GETTERS--&--SETTERS-------------------------------------------------
     */

}
